package com.longxi.tianjintianbao.ui.presenter.news;

import android.content.Intent;

import com.longxi.tianjintianbao.AppConst;
import com.longxi.tianjintianbao.MyApplication;
import com.longxi.tianjintianbao.R;
import com.longxi.tianjintianbao.api.ApiRetrofit;
import com.longxi.tianjintianbao.api.response.HttpResult;
import com.longxi.tianjintianbao.exception.ExceptionHandle;
import com.longxi.tianjintianbao.exception.MySubscriber;
import com.longxi.tianjintianbao.listener.AdapterListener;
import com.longxi.tianjintianbao.listener.SelectTaskListListener;
import com.longxi.tianjintianbao.model.task.Task;
import com.longxi.tianjintianbao.model.work.News;
import com.longxi.tianjintianbao.ui.activity.news.NewDetailActivity;
import com.longxi.tianjintianbao.ui.activity.task.TaskDetailActivity;
import com.longxi.tianjintianbao.ui.adapter.multitype.TaskListViewBinder;
import com.longxi.tianjintianbao.ui.adapter.multitype.news.NewsViewBinder;
import com.longxi.tianjintianbao.ui.base.BaseActivity;
import com.longxi.tianjintianbao.ui.base.BasePresenter;
import com.longxi.tianjintianbao.ui.view.news.INewsAtView;
import com.longxi.tianjintianbao.ui.view.task.ITaskListAtView;
import com.longxi.tianjintianbao.utils.MyUtils;
import com.longxi.tianjintianbao.utils.NListRefresh;

import org.json.JSONException;
import org.json.JSONObject;

import me.drakeet.multitype.Items;
import me.drakeet.multitype.MultiTypeAdapter;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

public class NewsAtPresenter extends BasePresenter<INewsAtView> {

    private MultiTypeAdapter adapter;
    private Items items;

    public NewsAtPresenter(BaseActivity context) {
        super(context);
    }

    public void initAdapter(){
        if(adapter == null){
            adapter = new MultiTypeAdapter();
            /* 注册类型和 View 的对应关系 */
            adapter.register(News.class, new NewsViewBinder(listener));
            getView().getRvContent().setAdapter(adapter);
        }
    }

    public void setBar() {
        mContext.setTitle(MyUtils.getResource().getString(R.string.xwtz));
        mContext.setTvLeft(MyUtils.getString(R.string.home));
    }

    /**
     * 测试数据
     */
    public void newsBulletin(){
        JSONObject object = new JSONObject();
        ApiRetrofit.getInstance().newsBulletin(object)
        .subscribeOn(Schedulers.io())
        .observeOn(AndroidSchedulers.mainThread())
        .subscribe(new MySubscriber<HttpResult<News>>(mContext) {
            @Override
            public void onError(ExceptionHandle.ResponeThrowable throwable) {
                mContext.hideWaitingDialog();
                MyUtils.showToast(throwable.message);
            }

            @Override
            public void onNext(HttpResult<News> result) {
                items = NListRefresh.getInstance().fjData(result,
                        getView().getRefreshLayout(),
                        items,
                        adapter,
                        mContext);
            }
        });
    }

    AdapterListener listener = new AdapterListener() {

        @Override
        public void onClick(int position) {
            News news = (News) items.get(position);
            Intent intent = new Intent(mContext, NewDetailActivity.class);
            intent.putExtra(AppConst.NEWS, news);
            mContext.startActivity(intent);
        }

        @Override
        public void onDelete(int position) {

        }
    };

    @Override
    public void detachView() {
        super.detachView();
        this.adapter = null;
        this.items = null;
        this.listener = null;
    }
}
